<?php
session_start();
include ('config.inc');
$errmsg=array();
$errflag=false;
//Connect to mysql server
	$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
	if(!$link) {
		die('Failed to connect to server: ' . mysql_error());
	}
	
	//Select database
	$db = mysql_select_db(DB_DATABASE);
	if(!$db) {
		die("Unable to select database");
	}
function clean($str){
	if(get_magic_quotes_gpc()){
		$str=stripslashes($str);
		
	}
	return mysql_real_escape_string($str);
	
}
$confirm_code=md5(uniqid(rand()));
$fname=clean($_POST['firstname']);
$lname=clean($_POST['lastname']);
$email=clean($_POST['youremail']);
$re_email=clean($_POST['reenteremail']);
$pass=clean($_POST['password']);
$gender=$_POST['gender'];
if($fname==''){
$errmsg[]='First Name is Missing';
$errflag=true;	
}
if($lname==''){
$errmsg[]='Last Name is Missing';
$errflag=true;	
}
if($email==''){
$errmsg[]='Email Address is Missing';
$errflag=true;	
}
if($re_email==''){
$errmsg[]='We need to know wether the Email Addresses are matching or Not!';
$errflag=true;	
}
if($pass==''){
$errmsg[]='Any Account should have a Password';
$errflag=true;	
}

if(strcmp($email, $re_email) !=0){
$errmsg[]='Email Addresses do not Match';
$errflag=true;	
}
if($email !=''){
	$qry=mysql_query("SELECT * FROM members WHERE email='$email'");
	if($qry){
		if(mysql_num_rows($qry) > 0){
			$errmsg[]='This Email address is already exists! Try a new One';
			$errflag=true;
		}
	}
	else{
		die("Query Failed!");
	}
}
	if($errflag){
		$_SESSION['ERR_MSG']=$errmsg;
		header('location: ../signup.php');
		exit();
	}
	$insert=mysql_query("INSERT INTO tem_members VALUES('$confirm_code','$fname','$lname','$email','$pass','$gender')");
	if($insert){
		echo "Inserted.";
	}else{
		
		echo "Error Occured" . mysql_error();
	}
	
	if($insert){
		$to=$email;
		$subject="Confirmation Link! - Khareedary";
		$header="from:bbaheer@technation.af";
		$message="In Order to Access the cPanle you have to confirm your Email address by clicking the below Link!";
		$message.="http://abade.technation.af/confirmation.php?passkey=$confirm_code";
		$sentmail=mail($to,$subject,$message,$header);
		if($sentmail){
			$_SESSION['SUCCESS']='Thank you for your Registration! Go to your Email, and We have Inboxed you a message to Confirma your Email Address';
			header('location: ../signup.php');
		}
	else{
		$_SESSION['SUCCESS_ERR']='Unable to Send you a Confirmation Link';
		session_write_close();
		header('location: ../signup.php');	
	}
	}//End of If
	
?>